#include "airplane.h"
#include <QPushButton>

airplane::airplane(QWidget *parent)
    : QWidget(parent),stop(true)
{
    planeImg=new QPixmap("plane.png");
    int width=planeImg->width()/10;
    int height=planeImg->height()/10;
    planeImgRect=new QRect(10,10,width,height);
    planeTimer=new QTimer(this);
    connect(this,SIGNAL(clicked()),this,SLOT(mousePressEventSLOT()));
    connect(planeTimer,SIGNAL(timeout()),this, SLOT(run()));

}

void airplane::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.drawPixmap(*planeImgRect,*planeImg);//在画框中画图，将画框添加到页面在添加按钮信号即可实现按钮控制图画运动。
}

void airplane::mousePressEvent(QMouseEvent *event)
{
    if(planeImgRect->contains(event->x(),event->y()))
    {
        stop=!stop;
        emit clicked();
    }
}
void airplane::run()
{
    int width=planeImg->width();
    int height=planeImg->height();
    planeImgRect->setX(planeImgRect->x()+2); //画框不断运动
    planeImgRect->setY(planeImgRect->y()+1);
    planeImgRect->setWidth(width/10);
    planeImgRect->setHeight(height/10);
    repaint();
}

void airplane::mousePressEventSLOT()
{
    if(!stop)
    {
        planeTimer->start(20);
    }
    else
    {
        planeTimer->stop();
    }
}

airplane::~airplane()
{
    delete planeImg;
    delete planeImgRect;
    delete planeTimer;
}
